/*
 * AerogelSD.cc
 *
 *  Created on: 15.02.2011
 *      Author: ed
 */

#include "TrackerSD.hh"
#include "Rtypes.h"
#include "G4VProcess.hh"
#include "DetectorConstruction.hh"
#include "PrimariesGenerator.hh"
#include "TFile.h"
#include "TBranch.h"
TrackerSD::TrackerSD(G4String name) : G4VSensitiveDetector(name)
{

	f = new TFile("~/tree1.root","recreate");
	tree = new TTree("aTree","Event tree");

	tree->Branch("numOfEvt",&event.particlesPerEvent,"numOfEvt/I");
	tree->Branch("lightOutput",&event.lightOutput,"lightOutput/I");
	tree->Branch("lAbs",&event.aerogelAbsorptionIndex,"lAbs/D");
	tree->Branch("lSc",&event.aerogelSc,"lSc/D");
	tree->Branch("R",&event.detectorRadius,"R/D");
	tree->Branch("lightCollection",&event.lightCollection,"lC/D");

}

TrackerSD::~TrackerSD()
{
	tree->Write();
}
G4bool TrackerSD::ProcessHits(G4Step*aStep,G4TouchableHistory*)
{


	if(aStep->GetPostStepPoint()->GetProcessDefinedStep()->GetProcessName()=="OpAbsorption")
	{
		event.lightOutput--;
	}

	if(aStep->GetPostStepPoint()->GetProcessDefinedStep()->GetProcessName()=="OpRayleigh")
	{
		event.nCollisions++;
	}
	return true;
}
void TrackerSD::Initialize(G4HCofThisEvent*)
{
	/*evt = new MyEvent(PrimariesGenerator::GetNParticlesPerEvent(),DetectorConstruction::GetSphereRadius(),
			PrimariesGenerator::GetPhotonEnergy(),DetectorConstruction::GetAbsorptionLength(),
			DetectorConstruction::GetScaterringLength());*/
	event.aerogelAbsorptionIndex = DetectorConstruction::GetAbsorptionLength();
	event.aerogelSc = DetectorConstruction::GetScaterringLength();
	event.detectorRadius = DetectorConstruction::GetSphereRadius();
	event.lightOutput = 300;
	event.nCollisions = 0;
	event.particlesPerEvent = 300;//PrimariesGenerator::GetNParticlesPerEvent();
	event.lightCollection = 0;
}
void TrackerSD::EndOfEvent(G4HCofThisEvent*)
{
	event.lightCollection = (G4double)event.lightOutput/(G4double)event.particlesPerEvent;
	tree->Fill();
}

void TrackerSD::DrawHist()
{
}
